package is.web.htgen.single;

import is.bean.department.*;
import is.bean.employee.Employee;
import is.bean.employee.EmployeeBeanRemoteHome;
import is.bean.region.Region;
import is.bean.region.RegionBeanRemoteHome;
import is.web.Checker;
import is.web.Locale;
import java.rmi.RemoteException;
import javax.ejb.FinderException;

/**
 * CRUD form generator
 *
 * @author aspr0
 */
public class DepartmentForm extends AbstractForm
{
    private final String rowsText =
            "<tr>"
            + "<td>Id</td>"
            + "<td>%s</td>"
            + "</tr>"
            + "<tr>"
            + "<td>Name</td>"
            + "<td>%s</td>"
            + "</tr>";
    private final String rowsSelect =
            "<tr>"
            + "<td>Parent department</td>"
            + "<td>%s</td>"
            + "</tr>"
            + "<tr>"
            + "<td>Region</td>"
            + "<td>%s</td>"
            + "</tr>"
            + "<tr>"
            + "<td>Boss</td>"
            + "<td>%s</td>"
            + "</tr>";
    private DepartmentBeanRemote bean;
    private DepartmentBeanRemoteHome departmentHome;
    private RegionBeanRemoteHome regionHome;
    private EmployeeBeanRemoteHome employeeHome;
    private Select<Department, DepartmentBeanRemote> select = new Select<Department, DepartmentBeanRemote>();

    public void setBean(DepartmentBeanRemote bean)
    {
        this.bean = bean;
    }

    public void setDepartmentHome(DepartmentBeanRemoteHome departmentHome)
    {
        this.departmentHome = departmentHome;
    }

    public void setEmployeeHome(EmployeeBeanRemoteHome employeeHome)
    {
        this.employeeHome = employeeHome;
    }

    public void setRegionHome(RegionBeanRemoteHome regionHome)
    {
        this.regionHome = regionHome;
    }

    public void setSelect(Select<Department, DepartmentBeanRemote> select)
    {
        this.select = select;
    }

    public String genCopyUpdate(boolean isUpdate) throws RemoteException, FinderException
    {
        Department department = bean.getData();

        Integer id = department.getId();
        String inputName = String.format(inputText, "name", department.getName());
        Integer parentId = department.getParentId();
        Integer regionId = department.getRegionId();
        Integer bossId = department.getBossId();

        select.setNullAllowed(true);
        select.setSelectName("parentId");
        select.setSelectedId(parentId);
        if (isUpdate)
        {
            select.setRemotes(departmentHome.findParentsAvailable(id));
        }
        else
        {
            select.setRemotes(departmentHome.findByExample(new Department()));
        }
        String parentSelect = select.generate();

        select.setNullAllowed(false);
        select.setSelectName("regionId");
        select.setSelectedId(regionId);
        select.setRemotes(regionHome.findByExample(new Region()));
        String regionSelect = select.generate();

        String bossSelect = "";
        if (isUpdate)
        {
            select.setNullAllowed(true);
            select.setSelectName("bossId");
            select.setSelectedId(bossId);
            Employee bossFilter = new Employee();
            bossFilter.setDepartmentId(id); /* Employees from current department only can become it's boss */
            select.setRemotes(employeeHome.findByExample(bossFilter));
            bossSelect = select.generate();
        }

        String rowsTextFilled = String.format(rowsText, id, inputName);
        String rowsSelectFilled = String.format(rowsSelect, parentSelect, regionSelect, bossSelect);
        String tableFilled = String.format(table, rowsTextFilled + rowsSelectFilled);


        String links = (isUpdate)
                ? String.format(linksDiv, linkTable + lD + linkRetrieve + lD + linkCopy + lD + linkDelete)
                : String.format(linksDiv, linkTable + lD + linkRetrieve + lD + linkUpdate + lD + linkDelete);
        links = String.format(links, id);

        return String.format(form, tableFilled + inputSubmit) + links;
    }

    public String genCreate() throws FinderException, RemoteException
    {
        String inputHiddenParam = String.format(inputHidden, "create", "create");
        String inputName = String.format(inputText, "name", "");
        String rowsTextFilled = String.format(rowsText, "", inputName);

        select.setNullAllowed(true);
        select.setSelectName("parentId");
        select.setRemotes(departmentHome.findByExample(new Department()));
        String parentDepartmentSelect = select.generate();

        select.setNullAllowed(false);
        select.setSelectName("regionId");
        select.setRemotes(regionHome.findByExample(new Region()));
        String regionSelect = select.generate();

//        select.setNullAllowed(true);
//        select.setSelectName("bossId");
//        select.setRemotes(employeeHome.findByExample(new Employee()));
//        String bossSelect = select.generate();

        String rowsSelectFilled = String.format(rowsSelect, parentDepartmentSelect, regionSelect, "");
        String tableFilled = String.format(table, rowsTextFilled + rowsSelectFilled);

        return String.format(form, inputHiddenParam + tableFilled + inputSubmit);

    }

    public String genDelete() throws RemoteException
    {
        Department department = bean.getData();

        Integer id = department.getId();
        String name = department.getName();

        String rowsTextFilled = String.format(rowsText, id, name);
        String tableFilled = String.format(table, rowsTextFilled);

        String links = String.format(linksDiv, linkTable + lD + linkRetrieve + lD + linkUpdate + lD + linkCopy);
        links = String.format(links, id);

        return String.format(form, tableFilled + inputSubmit) + links;
    }

    public String genRetrieve() throws RemoteException, FinderException
    {
        Department department = bean.getData();

        Integer id = department.getId();
        String name = department.getName();
        Integer parentId = department.getParentId();
        Integer regionId = department.getRegionId();
        Integer bossId = department.getBossId();

        String departmentLinkCanvas = "<a href=\"../department?id=%s&act=retrieve\">%s</a>";
        String parentLink = (Checker.isPositiveInteger(parentId))
                ? String.format(departmentLinkCanvas,
                                parentId,
                                departmentHome.findByPrimaryKey(parentId).getData().getName())
                : Locale.NULL_READABLE;

        String employeeLinkCanvas = "<a href=\"../employee?id=%s&act=retrieve\">%s</a>";
        String bossLink = (Checker.isPositiveInteger(bossId))
                ? String.format(employeeLinkCanvas,
                                bossId,
                                employeeHome.findByPrimaryKey(bossId).getData().getName())
                : Locale.NULL_READABLE;

        String regionLinkCanvas = "<a href=\"../region?id=%s&act=retrieve\">%s</a>";
        String regionLink = String.format(regionLinkCanvas,
                                          regionId,
                                          regionHome.findByPrimaryKey(regionId).getData().getName());

        String rowsTextFilled = String.format(rowsText, id, name);
        String rowsSelectFilled = String.format(rowsSelect, parentLink, regionLink, bossLink);
        String links = String.format(linksDiv, linkTable + lD + linkUpdate + lD + linkCopy + lD + linkDelete);
        links = String.format(links, id);

        return String.format(table, rowsTextFilled + rowsSelectFilled) + links;
    }
}
